using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using Object = UnityEngine.Object;

namespace FGUIDReplace
{
    public class FGUIDReplaceWindow : EditorWindow
    {
        /// <summary> 替换树图 </summary>
        ReplaceTreeView _treeView;

        /// <summary> 替换树图状态 </summary>
        [SerializeField] TreeViewState _treeViewState;


        /// <summary> 旧资源 </summary>
        Object _oldObject
        {
            get => mOldObject;
            set
            {
                if (value == null)
                {
                    mOldObject = null;
                    return;
                }

                if (value.GetType() == typeof(DefaultAsset))
                {
                    Debug.LogWarning("目录不支持替换操作！");
                    return;
                }

                if (_newObject != null && value.GetType() != _newObject.GetType())
                {
                    mOldObject = null;
                    Debug.LogWarning("资源类型不一致！");
                    return;
                }

                if (value == _newObject)
                {
                    Debug.LogWarning("同一个资源不需要替换！");
                    mOldObject = null;
                    return;
                }

                mOldObject = value;
            }
        }

        Object mOldObject;


        /// <summary> 需要替换的新资源 </summary>
        Object _newObject
        {
            get => mNewObject;
            set
            {
                if (value == null)
                {
                    mNewObject = null;
                    return;
                }

                if (value.GetType() == typeof(DefaultAsset))
                {
                    Debug.LogWarning("目录不支持替换操作！");
                    return;
                }

                if (_oldObject != null && value.GetType() != _oldObject.GetType())
                {
                    mNewObject = null;
                    Debug.LogWarning("资源类型不一致！");
                    return;
                }


                if (value == _oldObject)
                {
                    Debug.LogWarning("同一个资源不需要替换！");
                    mNewObject = null;
                    return;
                }

                mNewObject = value;
            }
        }

        Object mNewObject;

        /// <summary> 替换目录列表 </summary>
        [SerializeField] List<DefaultAsset> _replaceDirs = new();

        /// <summary> 添加图标 </summary>
        GUIContent _addIcon;

        /// <summary> 移除图标 </summary>
        GUIContent _closeIcon;

        /// <summary> 箭头图标 </summary>
        GUIContent _arrowsIcon;


        [MenuItem("Window/FTools/GUID Replace", false, 2000)]
        public static void ShowWindow()
        {
            GetWindow<FGUIDReplaceWindow>("GUID 引用替换");
        }

        void OnEnable()
        {
            _treeViewState ??= new TreeViewState();
            _treeView ??= new ReplaceTreeView(_treeViewState);
        }

        void OnGUI()
        {
            minSize = new Vector2(700, 350);

            if (_addIcon == null)
            {
                _addIcon = EditorGUIUtility.IconContent("winbtn_mac_max_h");
                _addIcon.text = string.Empty;
            }

            if (_closeIcon == null)
            {
                _closeIcon = EditorGUIUtility.IconContent("d_winbtn_mac_close_h");
                _closeIcon.text = string.Empty;
            }

            if (_arrowsIcon == null)
            {
                _arrowsIcon = EditorGUIUtility.IconContent("AreaEffector2D Icon");
                _arrowsIcon.text = string.Empty;
            }

            GUILayout.BeginHorizontal("FrameBox");
            {
                GUILayout.BeginVertical();
                {
                    GUILayout.Label("替换目录");
                    var btnRect = GUILayoutUtility.GetLastRect();
                    btnRect = new Rect(position.width - 44, btnRect.y, 35, 18);
                    if (GUI.Button(btnRect, _addIcon))
                        _replaceDirs.Add(null);

                    for (int i = 0; i < _replaceDirs.Count; i++)
                    {
                        GUILayout.BeginHorizontal();
                        {
                            _replaceDirs[i] = (DefaultAsset)EditorGUILayout.ObjectField("", _replaceDirs[i], typeof(DefaultAsset), false);
                            if (GUILayout.Button(_closeIcon, GUILayout.Width(35), GUILayout.Height(18)))
                                _replaceDirs.RemoveAt(i);
                        }
                        GUILayout.EndHorizontal();
                    }
                }
                GUILayout.EndVertical();
            }
            GUILayout.EndHorizontal();

            var rect = GUILayoutUtility.GetRect(0, position.width, 0, position.height);
            rect = new Rect(rect.x + 5, rect.y + 5, rect.width - 10, rect.height - 10);

            _treeView.OnGUI(rect);

            GUILayout.FlexibleSpace();

            GUILayout.BeginVertical("FrameBox");
            {
                GUILayout.BeginHorizontal();
                {
                    EditorGUIUtility.labelWidth = 40;
                    _oldObject = EditorGUILayout.ObjectField("查找", _oldObject, typeof(Object), false);
                    GUILayout.Label(_arrowsIcon, GUILayout.Width(20), GUILayout.Height(20));
                    _newObject = EditorGUILayout.ObjectField("替换", _newObject, typeof(Object), false);
                }

                GUILayout.EndHorizontal();
                {
                    if (_replaceDirs.Count < 1 || _oldObject == null)
                        GUI.enabled = false;

                    GUILayout.BeginHorizontal();
                    if (GUILayout.Button("查找", GUILayout.Height(30)))
                        GUIDReplace.SearchInclude(_treeView,_replaceDirs, _oldObject);

                    GUI.enabled = true;

                    if (_oldObject == null || _newObject == null || _treeView.GetRows().Count < 1)
                        GUI.enabled = false;

                    if (GUILayout.Button("替换", GUILayout.Height(30)))
                        GUIDReplace.Replace(_treeView, _oldObject, _newObject);
                }
                GUILayout.EndHorizontal();

                GUI.enabled = true;
            }
            GUILayout.EndVertical();
        }
    }
}